package com.wd.home.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.wd.common.bean.ManyBean
import com.wd.common.viewmodel.MyViewModel
import com.wd.home.R
import java.text.SimpleDateFormat
import java.util.Date

/**
 * 作者:孙殿彭 by mac on2023/9/12 15:25
 * 类注释:
 */
class ManyRecAdapter (private var context: Context,private var list: ManyBean)
    : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if(viewType==1){
            val inflater=LayoutInflater.from(context).inflate(R.layout.item_manyrecone,parent,false)
            return ViewHolder(inflater)
        }else if(viewType==3){
            val inflate = LayoutInflater.from(context).inflate(R.layout.item_manyrecthree, parent, false)
            return ThreeViewHolder(inflate)
        }else if(viewType==2){
            val inflate = LayoutInflater.from(context).inflate(R.layout.item_manyrectwo, parent, false)
            return VideoViewHolder(inflate)
        }else{
            val inflate = LayoutInflater.from(context).inflate(R.layout.item_manyrecone, parent, false)
            return ViewHolder(inflate)
        }
    }
    override fun getItemCount(): Int {
        return list.result.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if(holder is ViewHolder){
            var viewHolder=(holder)as ManyRecAdapter.ViewHolder
            viewHolder.name.setText(list.result[position].title)
            viewHolder.nameone.setText(list.result[position].nickName)
            viewHolder.nameone22.setText(list.result[position].publishLocation)
            viewHolder.nameone33.text="${SimpleDateFormat("yyyy-MM-dd").format(Date(list.result[position].releaseTime))}"


            Glide.with(context)
                .load(list.result[position].mediaUrl)
                .placeholder(R.mipmap.home_cartwo)
                .into(viewHolder.imgg)
        }else if(holder is ThreeViewHolder){
            var threeView=(holder) as ManyRecAdapter.ThreeViewHolder
            threeView.name2.setText(list.result[position].title)
            threeView.namethree.setText(list.result[position].nickName)
            threeView.namethree22.setText(list.result[position].publishLocation)
            threeView.namethree33.text="${SimpleDateFormat("yyyy-MM-dd").format(Date(list.result[position].releaseTime))}"

            Glide.with(context)//第1张图片
                .load(list.result[position].mediaUrl)
                .placeholder(R.mipmap.home_cartwo)
                .into(threeView.img1)

            Glide.with(context)//第2张图片
                .load(list.result[position].mediaUrl)
                .placeholder(R.mipmap.home_cartwo)
                .into(threeView.img2)

            Glide.with(context)//第3张图片
                .load(list.result[position].mediaUrl)
                .placeholder(R.mipmap.home_cartwo)
                .into(threeView.img3)
        }else if(holder is VideoViewHolder){
            var videoView=(holder) as ManyRecAdapter.VideoViewHolder
            videoView.name3.setText(list.result[position].title)

            videoView.gsyplayerid.setUp(list.result[position].mediaUrl,false,"")
            videoView.shipinzizizi.text="${SimpleDateFormat("yyyy-MM-dd").format(Date(list.result[position].releaseTime))}"
            videoView.shipinzizi.setText(list.result.get(position).nickName)
        }
    }
    class ViewHolder(view: View):RecyclerView.ViewHolder(view){
        val name: TextView
        val imgg: ImageView
        val nameone: TextView
        val nameone22: TextView
        val nameone33: TextView
        init {
            name = view.findViewById<TextView>(R.id.manyreconeneirong)
            imgg = view.findViewById<ImageView>(R.id.manyreconeimg)
            nameone = view.findViewById<TextView>(R.id.manyreconezi)
            nameone22 = view.findViewById<TextView>(R.id.manyreconeneirong22)
            nameone33 = view.findViewById<TextView>(R.id.manyreconeneirong33)
        }
    }
    class VideoViewHolder(view: View):RecyclerView.ViewHolder(view){
        val name3:TextView
        val shipinzizi:TextView
        val shipinzizizi:TextView

        val gsyplayerid:StandardGSYVideoPlayer
        init {
            name3 = view.findViewById<TextView>(R.id.manyrectwozi)
            shipinzizi = view.findViewById<TextView>(R.id.shipinzizi)
            shipinzizizi = view.findViewById<TextView>(R.id.shipinzizizi)
            gsyplayerid = view.findViewById<StandardGSYVideoPlayer>(R.id.gsyplayerid)
        }
    }
    class ThreeViewHolder(view: View):RecyclerView.ViewHolder(view){
        val name2:TextView
        val img1:ImageView
        val img2:ImageView
        val img3:ImageView
        val namethree:TextView
        val namethree22:TextView
        val namethree33:TextView
        init {
            name2 = view.findViewById<TextView>(R.id.manyrecthreezi)
            img1 = view.findViewById<ImageView>(R.id.manyrecthreeimg)
            img2 = view.findViewById<ImageView>(R.id.manyrecthreeimg2)
            img3 = view.findViewById<ImageView>(R.id.manyrecthreeimg3)
            namethree = view.findViewById<TextView>(R.id.manyrecthreeneirong)
            namethree22 = view.findViewById<TextView>(R.id.manyrecthreeneirong22)
            namethree33 = view.findViewById<TextView>(R.id.manyrecthreeneirong33)
        }
    }
    override fun getItemViewType(position: Int): Int {//多条目
        if(list.result[position].type==1){
            if(list.result[position].mediaUrl==null||list.result[position].mediaUrl?.split(",")?.size!!>1){
                return 3
            }else{
                return 1
            }
        }else{
            return 2
        }

    }

}